package com.ynny.controller;

import com.ynny.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class DataParamTestController {

    //原生的servlet获取参数的方法
    @GetMapping("/test01")
    @ResponseBody
    public String test01(HttpServletRequest request){

        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String age = request.getParameter("age");

        System.out.println("id = " + id);
        System.out.println("name = " + name);
        System.out.println("age = " + age);

        return "test01";
    }

    //SpringMVC获取参数的方法
    @GetMapping("/test02")
    @ResponseBody
    public String test02(@RequestParam(name = "id") String id,
                         @RequestParam(name = "name") String name,
                         @RequestParam(name = "age") Integer age){

        System.out.println("id = " + id);
        System.out.println("name = " + name);
        System.out.println("age = " + age);


        return "test02";
    }


    //使用对象的方式获取参数
    @GetMapping("/test03")
    @ResponseBody
    public String test03(User user){
        System.out.println("user = " + user);
        return "test03";
    }


    @GetMapping("/test04/{id}/{name}/{age}")
    @ResponseBody
    public String test04(@PathVariable(name = "id") Integer id,
                         @PathVariable(name = "name") String name,
                         @PathVariable(name = "age") Integer age){
        System.out.println("id = " + id);
        System.out.println("name = " + name);
        System.out.println("age = " + age);

        return "test04";
    }

    //使用map的方式获取参数
    @GetMapping("/test05")
    @ResponseBody
    public String test05(@RequestParam Map<String, Object> map){
        System.out.println("map = " + map);
        return "test05";
    }

}
